﻿if (typeof (Admin) == 'undefined')
    Admin = {};
if (typeof (Admin.Order) == 'undefined')
    Admin.Order = {};

Admin.Order = {
    _inProcess: function () {
        var ret = -1;
        console.log($("#cbxInProcess").attr("checked"));
        if ($("#cbxInProcess").attr("checked") == true)
            ret = 1;
        return ret;
    },
    _isFinish: function () {
        var ret = -1;
        if ($("#cbxFinish").attr("checked") == true)
            ret = 1;
        return ret;
    },
    _isCancel: function () {
        var ret = -1;
        if ($("#cbxCancel").attr("checked") == true)
            ret = 1;
        return ret;
    },
    _fromDate: function () { return $("#txtFromDate").val() },
    _toDate: function () { return $("#txtToDate").val() },
    _pageMax: function () { return $("#pageMax").val() },
    _pageIndex: function () { return $("#pageIndex").val() },
    _options: {
        id: 'gridList',
        url: function () {
            var ret = "Service/ListOrder.ashx?key=e"

            var iprocess = Admin.Order._inProcess();
            var isf = Admin.Order._isFinish();
            var ic = Admin.Order._isCancel();
            var fd = Admin.Order._fromDate();
            var td = Admin.Order._toDate();
            var pm = Admin.Order._pageMax();
            var pi = Admin.Order._pageIndex();

            if (pm.length > 0)
                ret += "&pm=" + pm;
            if (pi.length > 0)
                ret += "&pi=" + pi;
            console.log(iprocess);
            if (iprocess > 0)
                ret += "&if=0";
            if (isf > 0)
                ret += "&if=1";
            if (ic > 0)
                ret += "&ic=" + ic;
            if (fd.length > 0)
                ret += "&fd=" + fd;
            if (td.length > 0)
                ret += "&td=" + td;
            return ret;
        }
    },
    ManipulateData: function (options) {
        var obj = $("#" + options.id + " > tbody");
        var data = new TIO().ajax(options.url());
        var result = "";
        for (i = 0; i < data.length; i++) {
            var item = data[i];
            if (item.IsFinish != 0) {
                item.Status = "<span style='color: green;'>finished</span>";
            }
            else {
                item.Status = "<span style='color: red;'>in process</span>";
            }
            if (item.IsCancel == 1) {
                item.Status = "<span style='color: grey;'>canceled</span>";
            }
            result += $.Format(_formatOrder, item.OrderId, item.UserName, item.Status);
        }
        obj.html(result);
    },
    Calendar: function () {
        $("input#txtFromDate,input#txtToDate").datepicker({
            changeMonth: true,
            changeYear: true,
            onSelect: function (dateText, inst) {
                $('#' + inst.id).focus();
            }
        });
    },
    Detail: function (orderid) {
        var height = window.screen.height - 130;
        window.open("EditOrder.aspx?oi=" + orderid, "_blank", "resizable=1,width=785,height=" + height);
    },
    Finish: function (orderid) {
        var data = new TIO().ajax("Service/FinishOrder.ashx?oi=" + orderid);
        this.ManipulateData(this._options);
    },
    Cancel: function (orderid) {
        var data = new TIO().ajax("Service/CancelOrder.ashx?oi=" + orderid);
        this.ManipulateData(this._options);
    },
    EventCheckBoxStatus: function (obj) {
        $('input:radio').each(function () {
            $(this).attr("checked", "");
        });
        $(obj).attr("checked", "checked");
    },
    InitEvent: function () {
        var thisObj = this;
        $("#pageIndex").keyup(function (e) {
            var key = e.charCode || e.keyCode || 0;
            if (key == 48 || key == 49 || key == 50 || key == 51 || key == 52 || key == 53 ||
                key == 54 || key == 55 || key == 56 || key == 57) {
                thisObj.ManipulateData(thisObj._options);
            }
        });
        $("#pageMax").change(function () {
            thisObj.ManipulateData(thisObj._options);
        });
        $("#btnFilter").click(function () {
            thisObj.ManipulateData(thisObj._options);
            return false;
        });
    },
    Init: function () {
        this.InitEvent();
        this.Calendar();
        this.ManipulateData(this._options);
    }
}
$(function () {
    Admin.Order.Init();
});

_formatOrder = "<tr>" +
"<td class='a-center'>{0}</td>" +
"<td><a href='#'>{1}</a></td>" +
"<td>{2}</td>" +
"<td>" +
    "<a pid='{0}' href='javascript:Admin.Order.Detail({0});'><img src='../../img/icons/user.png' title='Show order detail' width='16' height='16' /></a>" +
    "<a pid='{0}' href='javascript:Admin.Order.Finish({0});'><img src='../../img/icons/user_edit.png' title='Order finished' width='16' height='16' /></a>" +
    "<a pid='{0}' href='javascript:Admin.Order.Cancel({0});'><img src='../../img/icons/user_delete.png' title='Cancel this Order' width='16' height='16' /></a>" +
"</td>" +
"</tr>";